#include "ImageManager.h"

sf::Image _ImageManager::GetImage(std::string filename)
{
	std::cout << "Loading texture \"" << filename << "\"" << std::endl;

	// Check if we already have an image loaded with the same file
	for (int i = 0; i < Images_name.size(); i++)
	{
		if (Images_name[i] == filename)
		{
			// Return the image stored in the vector
			std::cout << "Loading texture from memory..." << std::endl;
			return Images[i];
		}
	}

	// Load a new image
	std::cout << "Loading texture from disk..." << std::endl;
	sf::Image img;
	if (!img.LoadFromFile(filename))
	{
		// Print debug message
		//std::cout << "ERROR texture loading instead..." << std::endl;

		// Show another texture if the specified texture does not exist on disk
		img.LoadFromFile("sprites/error.tga");
		// Never cache the error texture!
		// This opens up for future hot-loading of textures!
	}
	else
	{
		// Add it to the vectors
		Images.push_back(img);
		Images_name.push_back(filename);
	}

	return img;
}